<template>
  <div class="login-page">
    <div class="title">面经登录</div>
    <van-form @submit="onSubmit">
        <van-field
          v-model="username"
          name="username"
          label="用户名"
          placeholder="用户名"
          :rules="[
          { required: true, message: '请填写正确的用户名' },
          { pattern:/^\w{5,}$/, message: '请输入 5 位即以上的用户名' }
          ]"
        />
        <van-field
          v-model="password"
          type="password"
          name="password"
          label="密码"
          placeholder="密码"
          :rules="[
          { required: true, message: '请填写正确的密码' },
          { pattern:/^\w{6,}$/, message: '请输入 6 位以上的密码' }
          ]"
        />
        <div style="margin: 16px;">
          <van-button round block type="info">提交</van-button>
        </div>
      </van-form>
      <router-link class="link" to="/register">注册账号</router-link>
  </div>
</template>

<script>
import { login } from '@/api/user'
import { setToken } from '@/utils/storage'
export default {
  name: 'LoginPage',
  data () {
    return {
      username: '',
      password: ''
    }
  },
  methods: {
    async onSubmit (values) {
      const res = await login(values)
      setToken(res.data.token)
      this.$toast.success('登录成功！')
      this.$router.push('/')
    }
  }
}
</script>

<style lang="less" scoped>
.title{
  font-size: 16px;
  height: 50px;
  line-height: 50px;
  text-align: center;
}
.link {
  color: #069;
  font-size: 12px;
  padding-right: 20px;
  float: right;
}
</style>
